<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">
		<template v-if="isLogin"> 
 
			<!-- 药剂分配 -->
			<view class="body">
				<view class="form-wrap m-b2">
					<view class="form-item">
						<view class="label">可分配数量：</view>
						<view class="form-input">
							<view class="content">
								<input type="digit" class="inp f-grey" v-model="params.sum" disabled placeholder="输入药剂名称" />
								<view class="link">{{params.pack_name}}</view>
							</view>
						</view>
					</view>
					
					<view class="form-item">
						<view class="label">分配单位：</view>
						<picker class="form-picker" mode="selector" :range="companys" range-key="realname" @change="changeCompany">
							<view class="picker">
								<input type="text" class="select-data t-l" v-model="params.company.name" placeholder="选择分配单位" disabled>
								<view class="icon">
									<view class="iconfont f-s68">&#xeb8a;</view>
								</view>
							</view>
						</picker>
					</view>				
			
					<view class="form-item">
						<view class="label">分配数量：</view>
						<view class="form-input">
							<view class="content">
								<input type="number" class="inp" v-model="params.num" placeholder="输入分配数量" />
								<view class="link">{{params.pack_name}}</view>
							</view>
						</view>
					</view>
			
				</view>
			</view>
			
			<!-- 底部 -->
			<view class="footer">
				<view class="fix-wrap bottom">
					<view class="btn" @click="submit">确定分配</view>
				</view>
			</view>
			
			 
		</template>
	</view>
</template>

<script>
	import {
		dateFormat,
		uploadImage,
		previewImage,
		uploadToServe
	} from '@/common/util.js'
	export default {
		name: 'name',
		data() {
			return {
				type: '', //  1 药剂分配  2 添加分配单位   
				isLogin: true,
				isIphoneX: getApp().globalData.isIphoneX,
				userInfo:uni.getStorageSync('userInfo'), 
				isStop: true, 
				companys:[],
				params: {
					page:1,
					pack_name:'',
					sum:'',
					num:'',
					potion_id:'',
					specs_id:'',
					distribute_id:'',
					company:{
						id:'',
						name:''
					}
				} 
			}


		},
		async onLoad(params) {  
			this.params.pack_name=params.pack_name;
			this.params.sum=params.sum;
			this.params.potion_id=params.potion_id;
			this.params.specs_id=params.specs_id;
			this.params.distribute_id=params.distribute_id;
			this.isLogin = await this.$loginState('check');		
			if(this.isLogin){
				this.getMembers(); 
			} 
		},
		async onShow() {

		},
		methods: {
			
			// 获取单位列表
			getMembers:async function(){
				let res=await this.$api.ycj_api.getMembers({
					page: this.params.page,
					types: '1,2,3,4,5,6,7,9,10'
				}) 
				if(res.data.ret==0){
					this.companys=res.data.data;
				}
			},
			 
			 
			// 选择分配单位
			changeCompany:function(e){ 
				let idx=e.detail.value;
				this.params.company.name=this.companys[idx].realname;
				this.params.company.id=this.companys[idx].id;
			},
			
			 
			
			// 添加药剂
			submit:async function(){
				let _this=this;
				if(this.params.company.name==''){
					uni.showToast({
						icon: 'none',
						title: '请选择分配单位',
						mask: true,
						duration: 1000
					})
					return
				}
				if(this.params.num==''){
					uni.showToast({
						icon: 'none',
						title: '请输入分配数量',
						mask: true,
						duration: 1000
					})
					return
				}else if(parseInt(this.params.num)>parseInt(this.params.sum)){
					uni.showToast({
						icon: 'none',
						title: '分配数量超出可分配数量范围',
						mask: true,
						duration: 1000
					})
					return
				}else if(!(/(^[1-9]\d*$)/.test(this.params.num))){
					uni.showToast({
						icon: 'none',
						title: '请输入合法的分配数量',
						mask: true,
						duration: 1000
					})
					return
				}  
				uni.showLoading({
					title:"数据提交中..."
				})
				
				if(this.isStop){
					this.isStop=false;
					let res=await this.$api.ycj_api.addDistribute({
						potion_id: this.params.potion_id,
						specs_id: this.params.specs_id,
						potion_sum: this.params.num,
						fid: this.params.distribute_id,
						uid: this.params.company.id,
						f_uid: this.userInfo.id,
					}) 
					if(res.data.ret==0){
						uni.hideLoading();  
						this.$store.commit('UPDATE_YAOJI_SUM',1);   // 药剂剩余数量更新
						uni.showToast({
							icon: 'success',
							title: '分配成功',
							mask: true,
							duration: 2000,
							success() {
								setTimeout(()=>{
									_this.isStop=true;
									uni.navigateBack({
										delta:1
									})
								},2000)
							}
						})
					}else{
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							image:'../../static/images/error.png',
							title: res.data.message,
							mask: true,
							duration: 2000,
							success() {
								setTimeout(()=>{
									_this.isStop=true; 
								},2000)
							}
						})
					}
				}
				 
				
			}, 
 
		},
		components: {},

		computed: {
			stock_update_state(){				
				return this.$store.state.myYaojiSumUpdate;
			} 
		},
		watch: {
			 
		}

	}
</script>

<style lang="less">
	.form-wrap .label {
		width: 35%;
	}

	.form-wrap .content .link {
		color: #000;
	}

	.status {
		padding: 0 20rpx;
		color: #f00;
		font-size: 28rpx;
	}

	.btn {
		@h: 80rpx;
		width: 98%;
		margin: 10rpx auto;
		text-align: center;
		height: @h;
		line-height: @h;
		background: linear-gradient(#e96b5c, #ce3c2f);
		border-radius: 10rpx;
		color: #fff;
		font-size: 34rpx;
	}

	 
</style>
